import { replaceHtml, openSelfModel } from '../utils/util';
import { modelHTML } from './constant';
import locale from '../locale/locale';

// 
let isinitialAuditRejectionModel = false;
function initialAuditRejectionModel() {
    if (isinitialAuditRejectionModel) {
        $("#rejection-opinion").val("")
        return;
    }

    isinitialAuditRejectionModel = true;
    const _locale = locale();
    const locale_button = _locale.button;
    $("body").append(replaceHtml(modelHTML, {
        "id": "luckysheet-audit-rejection",
        "addclass": "luckysheet-audit-rejection",
        "title": "驳回",
        "content": `
            <p>驳回区域：<span id="audit-reject-range"></span></p>
           
            <div style="margin-top:20px;">
               <span style="display:inline-block;width:100px;">驳回意见：</span> <textarea id="rejection-opinion" type="textarea" maxlength=50 style="vertical-align: top;width:193px;"></textarea>
           </div>
        `,
        "botton": `<button id="luckysheet-audit-reject-confirm" class="btn btn-primary">${locale_button.confirm}</button>
                    <button class="btn btn-default luckysheet-model-close-btn">${locale_button.cancel}</button>`,
        "style": "z-index:1011;width:500px;"
    }));

    initialAuditRejectionEvent();
}
function initialAuditRejectionEvent() {
    $("#luckysheet-audit-reject-confirm").click(function () {
        let cellRangeFmt = []
        let arr = luckysheet.getRangeWithFlattenFilterMC().map(item => {
            cellRangeFmt.push(luckysheet.getTxtByRange({ column: [item.c, item.c], row: [item.r, item.r] }))
            return item.r + ',' + item.c
        })
        window.parent.postMessage({
            command: "rejection",
            rejectionIdea: $("#rejection-opinion").val(),
            cellRange: JSON.stringify(arr),
            cellRangeFmt: JSON.stringify(cellRangeFmt),
        }, "*")
        $("#luckysheet-audit-rejection").hide();
        $("#luckysheet-modal-dialog-mask").hide();
    })
}
export function openAuditRejectionModel() {
    initialAuditRejectionModel();
    openSelfModel("luckysheet-audit-rejection");
    $("#audit-reject-range").text(luckysheet.getRangeAxis().join(','));

}